package com.ncloud.chnl.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ncloud.chnl.common.Constant;
import com.ncloud.chnl.entity.CustArea;
import com.ncloud.chnl.mapper.CustAreaMapper;
import com.ncloud.chnl.service.CustAreaNodeService;
import com.ncloud.chnl.service.CustAreaService;
import com.ncloud.chnl.service.CustAreaUserService;
import com.ncloud.common.enmus.ResultEnum;
import com.ncloud.common.exception.UserException;
import com.ncloud.param.service.SequenceService;

@Service
public class CustAreaServiceImpl implements CustAreaService {

	@Reference
	private SequenceService sequence;
	@Autowired
	private CustAreaMapper mapper;
	@Autowired
	private CustAreaNodeService nodeService;
	@Autowired
	private CustAreaUserService userService;
	
	@Override
	public Object createCustAreaInfo(JSONObject json) {
		if(!json.containsKey("areaName") || !json.containsKey("custId")){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		CustArea custArea = JSON.parseObject(json.toJSONString(), CustArea.class);
		Long userId = json.getLong("userID");
		Long areaId = sequence.getSequence("CUST_AREA_ID_SEQ");
		custArea.setAreaId(areaId);
		custArea.setCreateUser(userId);
		custArea.setUpdateUser(userId);
		custArea.setCreateTime(new Date());
		custArea.setUpdateTime(new Date());
		custArea.setState(Constant.CHNL_COMMON_STATE_NORMAL);
		try {
			mapper.insertCustArea(custArea);
		} catch (Exception e) {
			throw new UserException(ResultEnum.DATABASE_ERROR+e.toString());
		}
		
		return HttpStatus.CREATED;
	}

	@Override
	public Object updateCustAreaInfo(JSONObject json) {
		if(!json.containsKey("areaId") || !json.containsKey("areaName")){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		Long areaId = json.getLong("areaId");
		Long userId = json.getLong("userID");
		CustArea custArea = mapper.getCustArea(areaId);
		custArea.setAreaName(json.getString("areaName"));
		custArea.setUpdateUser(userId);
		try {
			mapper.updateCustArea(custArea);
		} catch (Exception e) {
			throw new UserException(ResultEnum.DATABASE_ERROR+e.toString());
		}
		return HttpStatus.OK;
	}

	@Override
	public JSONArray getCustAreaList(JSONObject json) {
		if(!json.containsKey("custId")){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		JSONArray array = new JSONArray();
		Long custId = json.getLong("custId");
		List<CustArea> custAreas = mapper.getAllCustAreas(custId);
		String type = json.getString("type");
		if(StringUtils.isNotBlank(type) && "1".equals(type)){
			for (CustArea custArea : custAreas) {
				JSONObject object = JSON.parseObject(JSON.toJSONString(custArea));
				//获取节点
				JSONObject param = new JSONObject();
				param.put("areaId", custArea.getAreaId());
				JSONArray nodeArray = nodeService.getCustAreaNodeList(param);
				//获取人员
				JSONArray userArray = userService.getCustAreaUserList(param);
				object.put("nodes", nodeArray);
				object.put("users", userArray);
				array.add(object);
			}
		}else {
			array = JSON.parseArray(JSON.toJSONString(custAreas));
		}
		return array;
	}

	@Override
	public Object deleteCustAreaInfo(JSONObject json) {
		if(!json.containsKey("areaId") || StringUtils.isBlank(json.getString("areaId"))){
			throw new UserException(ResultEnum.PARAM_ERROR);
		}
		Long areaId = json.getLong("areaId");
		Long userId = json.getLong("userID");
		//删除分组
		CustArea custArea = mapper.getCustArea(areaId);
		if(custArea==null) throw new UserException("删除分组编码信息不存在!areaId="+areaId);
		custArea.setState(Constant.CHNL_COMMON_STATE_DELETE);
		custArea.setUpdateUser(userId);
		mapper.deleteCustAreaInfo(custArea);
		//删除分组和节点的关系
		JSONObject area = new JSONObject();
		area.put("areaId", areaId);
		area.put("userID", userId);
		nodeService.deleteCustAreaNodeByAreaId(area);
		userService.deleteCustAreaUserByAreaId(area);
		return HttpStatus.OK;
	}

}
